package com.industrial.white.handler.excel;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.industrial.system.api.domain.AdminUser;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.Objects;

public class TableTitleHandler implements SheetWriteHandler {

    private Integer titleMergeColumn;
    private AdminUser adminUser;

    public TableTitleHandler(Integer titleMergeColumn,AdminUser adminUser) {
        this.titleMergeColumn = titleMergeColumn;
        this.adminUser = adminUser;
    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        getTitle(workbook, sheet, 0,"企业信息采集表", (short) 800,(short) 400,false);
        sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, titleMergeColumn - 1));
//        填报单位：{city}工业和信息化局             联系人： {nickName}             联系电话：（座机） {landLine} /（手机）{phonennumber}
        getTitle(workbook, sheet, 1,String.format("填报单位：%s工业和信息化局        联系人： %s   联系电话：（座机） %s /（手机）%s "
                , Objects.isNull(adminUser.getCity()) ? "" : adminUser.getCity()
                ,Objects.isNull(adminUser.getNickName()) ? "" : adminUser.getNickName()
                ,Objects.isNull(adminUser.getLandLine()) ? "" : adminUser.getLandLine()
                ,Objects.isNull(adminUser.getPhonennumber()) ? "" : adminUser.getPhonennumber()), (short) 800,(short) 250,true);
        sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, titleMergeColumn - 1));
    }

    /**
     *  @param workbook workbook
     * @param sheet sheet
     * @param rowNum rowNum
     * @param titleName titleName
     * @param height height
     * @param fontHeight fontHeight
     */
    private void getTitle(Workbook workbook, Sheet sheet, int rowNum, String titleName, short height, short fontHeight,boolean left) {
        Row row2 = sheet.createRow(rowNum);
        getTitle(row2, workbook, titleName, height, fontHeight, 0,left);
    }

    /**
     * 同一Row对象（多个字段在同一行）
     * @param row2 row对象
     * @param workbook workbook
     * @param titleName 标题名
     * @param height 高
     * @param fontHeight 字高
     * @param cellNum 字体所在列位置
     */
    private void getTitle(Row row2, Workbook workbook, String titleName, short height, short fontHeight, int cellNum,boolean left) {
        row2.setHeight(height);
        Cell cell1 = row2.createCell(cellNum);
        cell1.setCellValue(titleName);
        CellStyle cellStyle = workbook.createCellStyle();

        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        if(left){
            cellStyle.setAlignment(HorizontalAlignment.LEFT);
        }else{
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
        }

        cellStyle.setWrapText(true);
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight(fontHeight);
        cellStyle.setFont(font);
        cell1.setCellStyle(cellStyle);
    }
}
